package com.example.random_clap.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.random_clap.common.R;
import com.example.random_clap.common.constant.MessageConstant;
import com.example.random_clap.entity.ProblemCategory;
import com.example.random_clap.service.ProblemCategoryService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 问题类别模块
 *
 * @author ouyangkai
 * @date 2022-10-29
 */

@RestController
@RequestMapping("/category")
public class ProblemCategoryController {

    @Autowired
    private ProblemCategoryService problemCategoryService;

    /**
     * 新增问题类别
     *
     * @param problemCategory
     * @return
     */
    @PostMapping("/saveCategory")
    public R<String> saveCategory(@RequestBody ProblemCategory problemCategory) {
        boolean res = problemCategoryService.save(problemCategory);
        if (!res) {
            return R.error(MessageConstant.ADD_PROBLEM_CATEGORY_FAIL);
        }
        return R.success(MessageConstant.ADD_PROBLEM_CATEGORY_SUCCESS);
    }

    /**
     * 编辑问题类别
     *
     * @param problemCategory
     * @return
     */
    @PutMapping("/updateCategory")
    public R<String> updateCategory(@RequestBody ProblemCategory problemCategory) {

        boolean res = problemCategoryService.updateById(problemCategory);

        if (!res) {
            return R.error(MessageConstant.UPDATE_PROBLEM_CATEGORY_FAIL);
        }

        return R.success(MessageConstant.UPDATE_PROBLEM_CATEGORY_SUCCESS);
    }

    /**
     * 根据id查询问题类别信息
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<ProblemCategory> getCategoryById(@PathVariable Long id) {

        ProblemCategory problemCategory = problemCategoryService.getById(id);

        if (problemCategory == null) {
            return R.error(MessageConstant.QUERY_PROBLEM_CATEGORY_FAIL);
        }
        return R.success(problemCategory);
    }


    /**
     * 根据条件分页查询问题类别信息
     *
     * @param currentPage
     * @param pageSize
     * @param queryString
     * @return
     */
    @GetMapping("/getCategoryPage")
    public R<Page<ProblemCategory>> getCategoryPage(int currentPage, int pageSize, String queryString) {
        Page<ProblemCategory> pageInfo = new Page<>(currentPage, pageSize);
        LambdaQueryWrapper<ProblemCategory> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(Strings.isNotBlank(queryString), ProblemCategory::getName, queryString);
        problemCategoryService.page(pageInfo, queryWrapper);
        return R.success(pageInfo);
    }

    /**
     * 根据id删除问题类别信息
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public R<String> deleteCategoryById(@PathVariable Long id) {
        boolean res = problemCategoryService.removeById(id);
        if (!res) {
            return R.error(MessageConstant.DELETE_PROBLEM_CATEGORY_FAIL);
        }
        return R.success(MessageConstant.DELETE_PROBLEM_CATEGORY_SUCCESS);
    }


}
